package com.wt.struggle.goodbye.main.adapter

import android.content.Context
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.Config
import com.wt.struggle.goodbye.base.ItemClickListener
import com.wt.struggle.goodbye.base.ProBaseAdapter
import com.wt.struggle.goodbye.info.NewsInfo
import com.wt.struggle.goodbye.main.activity.CommentListActivity
import com.xin.lv.yang.utils.utils.DataUtil
import com.xin.lv.yang.utils.utils.ImageUtil
import kotlinx.android.synthetic.main.comment_item.view.*

class CommentAdapter(context: Context, list: ArrayList<NewsInfo>) : ProBaseAdapter<NewsInfo>(context, list) {

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val vh = holder as VH

        val info = list[position]
        val userInfo = info.appUserExtraInfo

        if (userInfo != null) {
            vh.tvCommentName.text = userInfo.nickName
            ImageUtil.getInstance()
                .loadCircleImage(
                    context,
                    vh.imageCommentHead,
                    Config.GET_IMAGE_URL + userInfo.avatar,
                    R.drawable.user_head
                )
        }

        vh.tvCommentContent.text = DataUtil.getEmoji(context, info.content)
        vh.tvCommentTime.text = DataUtil.longToTime(info.createtime / 1000, "yyyy-MM-dd")

        val arr = info.subCommentInfoList

        vh.huiFuTextRecyclerView.layoutManager = LinearLayoutManager(context)
        vh.huiFuTextRecyclerView.isNestedScrollingEnabled = false

        if (arr != null && arr.size != 0) {

            vh.huiFuTextRecyclerView.visibility = View.VISIBLE

            val textAdapter = TextAdapter(context, info, arr)

            vh.huiFuTextRecyclerView.adapter = textAdapter

            textAdapter.itemClickListener = object : ItemClickListener {

                override fun onItemClick(position: Int) {
                    (context as CommentListActivity).commentSon(2, arr[position])
                }

                override fun onLongClick(position: Int) {
                    (context as CommentListActivity).showDelete(vh.itemView, arr[position])

                }

            }

        } else {
            vh.huiFuTextRecyclerView.visibility = View.GONE

        }


        vh.tvCommentReturn.setOnClickListener {
            // 回复评论
            (context as CommentListActivity).commentSon(2, info)
        }

    }

    override fun onCreateView(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return VH(inflater.inflate(R.layout.comment_item, parent, false))
    }

    override fun onUpdateHolder(holder: RecyclerView.ViewHolder, position: Int, payloads: MutableList<Any>) {
    }

    inner class VH(view: View) : RecyclerView.ViewHolder(view) {
        val tvCommentName = view.tvCommentName
        val tvCommentContent = view.tvCommentContent
        val imageCommentHead = view.imageCommentHead
        val tvCommentTime = view.tvCommentTime
        val huiFuTextRecyclerView = view.huiFuTextRecyclerView
        val tvCommentReturn = view.tvCommentReturn
        val contentLinear = view.contentLinear

    }
}